//
//  DefaultViewController.m
//  VideoSavePaw
//
//  Created by 王亮 on 2021/1/15.
//  Copyright © 2021 Game Collection. All rights reserved.
//

#import "ADViewController.h"
#import "BUPangleAdTools.h"
#import "AppDelegate.h"

@interface ADViewController ()
@property(nonatomic,strong) BUPangleAdTools *pangleAdTools;
@property(nonatomic,strong) UIView *buttonView;

@property(nonatomic,strong) UIView *showAdView;
@property(nonatomic,strong) UIImageView *iconImageView;
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) UILabel *descLabel;
@end

@implementation ADViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self initView];
    [self initAd];
}
-(void)initView{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app名称
    self.titleLabel.text = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    // app版本
    self.descLabel.text = [NSString stringWithFormat:@"V%@",[infoDictionary objectForKey:@"CFBundleShortVersionString"]];

    [self.view addSubview:self.showAdView];
    [self.view addSubview:self.buttonView];
    
    [self.showAdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.buttonView.mas_top).mas_offset(-0);
    }];
    [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view).mas_offset(-kBottomSafeHeight);
        make.height.mas_offset(80);
    }];
}
-(void)initAd{
    self.pangleAdTools.showAdView = self.showAdView;
    self.pangleAdTools.rootViewController = self;

    [self.pangleAdTools setAdEndBlock:^(Ad_Close_Type type) {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate reloadWindows];
    }];

    [self.pangleAdTools showOpenAd];
}





-(UIView *)showAdView{
    if (!_showAdView){
        _showAdView = [[UIView alloc]init];
        _showAdView.clipsToBounds = YES;
        
        UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [activityIndicator startAnimating];
        [_showAdView addSubview:activityIndicator];
        [activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(_showAdView);
        }];
    }
    return _showAdView;
}
-(UIView *)buttonView{
    if (!_buttonView){
        _buttonView = [[UIView alloc]init];
        
        [_buttonView addSubview:self.iconImageView];
        [_buttonView addSubview:self.titleLabel];
        [_buttonView addSubview:self.descLabel];
        
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_buttonView).mas_offset(-40);
            make.bottom.mas_equalTo(_buttonView).mas_offset(-15);
            make.size.mas_equalTo(CGSizeMake(50, 50));
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(15);
            make.top.mas_equalTo(self.iconImageView).mas_offset(5);
        }];
        [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(1);
        }];
        
    }
    return _buttonView;
}
-(UIImageView *)iconImageView{
    if (!_iconImageView){
        _iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"def_app_icon"]];
        _iconImageView.clipsToBounds = YES;
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        _iconImageView.layer.cornerRadius = 8;
    }
    return _iconImageView;
}
-(UILabel *)titleLabel{
    if (!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = HexColor(@"9E96B6");
        _titleLabel.font = [UIFont systemFontOfSize:18];
    }
    return _titleLabel;
}
-(UILabel *)descLabel{
    if (!_descLabel){
        _descLabel = [[UILabel alloc]init];
        _descLabel.textColor = HexColor(@"9E96B6");
        _descLabel.font = [UIFont systemFontOfSize:14];
    }
    return _descLabel;
}
-(BUPangleAdTools *)pangleAdTools{
    if (!_pangleAdTools){
        _pangleAdTools = [[BUPangleAdTools alloc]init];
    }
    return _pangleAdTools;
}

@end
